/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.device.modular.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import vip.xiaonuo.device.core.enums.Constants;
import vip.xiaonuo.device.core.enums.DeviceErrEnum;
import vip.xiaonuo.device.modular.entity.ApiLog;
import vip.xiaonuo.device.modular.service.DeviceApiService;
import vip.xiaonuo.device.vo.Result;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;

/**
 * 设备api日志Service接口实现类
 *
 * @author levin
 * @date 2022/11/30 13:27
 **/
@Service("deviceOfflineService")
public class DeviceOfflineServiceImpl extends DeviceBaseService {
    @Resource(name = "deviceInventoryService")
    private DeviceApiService deviceInventoryService;

    @Resource(name = "deviceDoorCloseService")
    private DeviceApiService deviceDoorCloseService;

    @Resource(name = "deviceDoorOpenService")
    private DeviceApiService deviceDoorOpenService;

    @Override
    protected Integer getReqType() {
        return Constants.OFFLINEDATA;
    }

    @Override
    protected void handleWithParentLog(ApiLog log, JSONObject params) {
        JSONArray data = params.getJSONArray("data");
        for (Object item : data) {  //开门、关门、盘库报文
            JSONObject itemJSONObject = JSONObject.parseObject(JSON.toJSONString(item));
            Object childData = itemJSONObject.get("data");
            if (childData instanceof Collection) {//盘库信息 inventory
                deviceInventoryService.execute(itemJSONObject, log.getId());
                continue;
            }
            JSONObject openOrCloseData = itemJSONObject.getJSONObject("data");
            boolean isOpenDoor = openOrCloseData.containsKey("opener");
            if (isOpenDoor) {//opendoor
                deviceDoorOpenService.execute(itemJSONObject, log.getId());
                continue;
            }
            //closedoor
            deviceDoorCloseService.execute(itemJSONObject, log.getId());
        }

    }

//    @Override
//    protected Result handle(JSONObject param) {
//        JSONArray data = param.getJSONArray("data");
//        for (Object item : data) {  //开门、关门、盘库报文
//            JSONObject itemJSONObject = JSONObject.parseObject(JSON.toJSONString(item));
//            Object childData = itemJSONObject.get("data");
//            if (childData instanceof Collection) {//盘库信息 inventory
//                deviceInventoryService.execute(itemJSONObject);
//                continue;
//            }
//            JSONObject openOrCloseData = itemJSONObject.getJSONObject("data");
//            boolean isOpenDoor = openOrCloseData.containsKey("opener");
//            if (isOpenDoor) {//opendoor
//                deviceDoorOpenService.execute(itemJSONObject);
//                continue;
//            }
//            //closedoor
//            deviceDoorCloseService.execute(itemJSONObject);
//        }
//        return Result.ok();
//    }
}
